#include "Timer.h"
#include <gl/freeglut.h>

#define getTime() ((float)glutGet(GLUT_ELAPSED_TIME) / 1000)

Timer::Timer()
{
	Start();
}

void Timer::Start()
{
	startTime = getTime();
	currentTime = 0.0f;
	elapsedTime = 0.0f;
	fps = 0;
}

void Timer::Idle()
{
	static GLint TempFPS = 0;
	static GLfloat previousFPSTime = 0.0f;

	GLfloat newCurrentTime;

	newCurrentTime = getTime() - startTime;
	elapsedTime = newCurrentTime - currentTime;

	++TempFPS;

	if (newCurrentTime - previousFPSTime > 1.0f)
	{
		previousFPSTime = newCurrentTime;
		fps = TempFPS;
		TempFPS = 0;
	}

	currentTime = newCurrentTime;
}

float Timer::getCurrentTime() const
{
	return currentTime;
}

float Timer::getElapsedTime() const
{
	return elapsedTime;
}

int Timer::getFPSCounter() const
{
	return fps;
}